package com.api.gbhg.lbj.dao;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.lbj.api.ReimbursementimportApi;
import com.api.gbhg.lbj.tool.Tools;

import com.api.gbhg.lbj.util.Budgetsources;
import com.api.gbhg.lbj.util.ProExpPertargetImpmonDetail;
import com.api.gbhg.zengping.common.res.Res;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.engine.board.web.DashboardAction;

import com.google.common.base.Joiner;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import static com.api.gbhg.lbj.tool.Tools.*;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO
 * @date 2021/3/13 14:00
 */
public class ReimbursementDao {

    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    private static final PropertyManagementCostDao rmcd = new PropertyManagementCostDao();

    /**
     * 添加报销明细
     *
     * @param mapDtl
     */
    public static void addDetail(Map<String, String>[] mapDtl, String flowHeadLine) {
        //计算报销总金额
        Double bxjeSum = 0.00;
        String remark = "";
        for (int i = 0; i < mapDtl.length; i++) {
            bxjeSum += Double.valueOf(mapDtl[i].get("money"));
            remark = mapDtl[i].get("remark").toString();
        }
        // 添加一条明细
        String lcjlid = String.valueOf(mapDtl[0].get("lcjlid"));
        RecordSet rs = rmcd.queryJingfeiBylcjlId(lcjlid);
        while (rs.next()) {
            Map<String, Object> param = new HashMap<>();
            param.put("mainid", rs.getInt("id"));
            param.put("bxje", bxjeSum);
            param.put("spzt", 0);
            // 报销人
            String bxr = rs.getStringNoTrim("modedatamodifier");
            param.put("bxr", bxr);
            // 报销人部门
            RecordSet sbdw = rmcd.getSBKSById(Integer.valueOf(bxr));
            sbdw.next();
            String departmentmark = sbdw.getString("departmentmark");
            logger.info("departmentmark" + departmentmark);
            param.put("bxrbm", Tools.getbmyId(departmentmark));
            //经办部门
            param.put("jbbm", "0");
            //经办人员
            param.put("jbry", "0");
            //申请日期
            param.put("sqrq", rs.getStringNoTrim("modedatamodifydatetime"));
            // 报销流程名称
            param.put("bxlcmc", flowHeadLine);
            //报销流程记录id
            String lcjlid1 = rs.getStringNoTrim("lcjlid");
            param.put("bxlcjlid", lcjlid1 == null ? "" : lcjlid1);
            // 流程编号
            String lcid = rs.getStringNoTrim("lcid");
            param.put("lcbh", lcid == null ? "" : lcid);
            param.put("bz", remark);
            // 新增一条明细
            rmcd.addJingfeiDetail(param);
            break;
        }
    }

    /**
     * @param param lcjlid、bmzcjjfl、money
     * @param map   （经费表）记录是哪条记录未成功  excle
     * @return
     * @throws Exception
     */
    public boolean queryJingfei1(Map<String, String>[] param, Map<String, String> map) throws Exception {
        RecordSetTrans rst = new RecordSetTrans();
        RecordSetTrans rs = new RecordSetTrans();
        //取消自动提交
        rst.setAutoCommit(false);
        for (int i = 0; i < param.length; i++) {
            //流程记录id
            String lcjlid = param[i].get("lcjlid");
            //部门支出经济分类
            String bmzcjjfl = param[i].get("bmzcjjfl");
            //报销金额
            Double money = Double.valueOf(param[i].get("money"));
            //金额转换，转为万元(数据库为万元)
            //money/=10000;
            // 查询语句
            String querySql = "SELECT syed,dj FROM uf_jingfei  WHERE lcjlid=" + lcjlid + " AND bmzcjjfl=" + bmzcjjfl + " ";
            rst.executeQuery(querySql);
            rst.next();
            double syed = rst.getDouble("syed");
            if (money < 0 || money > syed) {
                map.put("lcjlid", lcjlid);
                map.put("bmzcjjfl", bmzcjjfl);
                map.put("bxje", String.valueOf(money));
                map.put("syed", String.valueOf(syed));
                map.put("报销金额不得小于零", "剩余额度不足");
                continue;
            }
            String updateSql = "update uf_jingfei set syed=syed-" + money + " where lcjlid=" + lcjlid + " and bmzcjjfl=" + bmzcjjfl + "";
            logger.info("经费表冻结金额：-------------------->" + updateSql);
            rst.executeSql(updateSql);
            //String querySql="SELECT syed,dj FROM uf_jingfei  WHERE lcjlid="+lcjlid+" AND bmzcjjfl="+bmzcjjfl+" ";

            rs.executeQuery(querySql);
            rs.next();
            double syed1 = rs.getDouble("syed");
            if (syed1 < 0) {
                //剩余额度不足需要回滚
                //返回信息
                // 流程id
                map.put("lcjlid", lcjlid);
                map.put("bmzcjjfl", bmzcjjfl);
                map.put("bxje", String.valueOf(money));
                map.put("syed", String.valueOf(syed1));
                rst.rollback();
                return false;
            }
        }
        rst.commit();
        return true;
    }

    public static RecordSet JinfeiQuery(Budgetsources bud){
        RecordSet rs=new RecordSet();
        Integer ysly = bud.getYsly();
        Integer zcqd = bud.getZcqd();
        String fzysrfl = bud.getFzysrfl();
        String nd = bud.getNd();
        String lcjlid = bud.getYsxm();
        String gnkm = bud.getGnkm();
        String jbbm = bud.getJbbm();
        String zcdfczbkxm = bud.getZcdfczbkxm();
        String yslylx = ysly == 0 ? "当年预算核批" : "上年结转核批";
        String sql="";
        logger.info("经办部门:"+jbbm+",预算来源lcjlid:" + lcjlid + ",支出渠道:" + zcqd + "预算来源:" + ysly + "非中央收入分类:" + fzysrfl);
        if (zcqd==0){
            sql = "select sum(syed) hj,id,je,syed,djje,yslylx,lcid,srlx,zcqd,dfczbkxm,yslylx,nd from uf_jingfeilaiyuan where lcid=" + lcjlid + " and yslylx=" + "'" + yslylx + "'"+" group by id,je,syed,djje,yslylx,lcid,srlx,zcqd,dfczbkxm,yslylx,nd order by id desc";
        }else if (zcqd==1&&fzysrfl.equals("8")||zcqd==2&&fzysrfl.equals("5")){
            sql = "select sum(syed) hj,id,je,syed,djje,yslylx,lcid,srlx,zcqd,dfczbkxm,yslylx,nd from uf_jingfeilaiyuan where lcid=" + lcjlid + " and yslylx=" + "'" + yslylx + "'" + " and zcqd=" + fzysrfl +"  and dfczbkxm="+zcdfczbkxm+" group by id,je,syed,djje,yslylx,lcid,srlx,zcqd,dfczbkxm,yslylx,nd order by id desc";
        }else {
            sql = "select sum(syed) hj,id,je,syed,djje,yslylx,lcid,srlx,zcqd,dfczbkxm,yslylx,nd from uf_jingfeilaiyuan where lcid=" + lcjlid + " and yslylx=" + "'" + yslylx + "'" + " and zcqd=" + fzysrfl +" group by id,je,syed,djje,yslylx,lcid,srlx,zcqd,dfczbkxm,yslylx,nd order by id desc";
        }
        logger.info("报销校验查询报销经费来源sql："+sql);
        rs.executeQuery(sql);
        return rs;
    }
    /**
     * @description: TODO 中央财政拨款收入来源
     * @author
     * @date
     * @version 1.0
     */
    public static RecordSet zyJinfeiQuery(Budgetsources bud){
        RecordSet rs=new RecordSet();
        String lcjlid = bud.getYsxm();
        String sql = "select * from uf_gxmdygnkm where xmlcid=" + lcjlid;
        //logger.info("报销中央财政拨款收入来源uf_gxmdygnkm-sql："+sql);
        rs.executeQuery(sql);
        return rs;
    }

    public String getXmdw(String lcjlid){
        RecordSet rs = new RecordSet();
        rs.execute("select dlhsdw from uf_jingfei_version where requestid="+lcjlid);
        rs.next();
        return rs.getString("dlhsdw");
    }
    /**
     *  TODO 报销扣减明细表冻结(会计节点后)
     * @param param
     * @param flag true为归档扣减 false为会计后冻结
     * @return
     */
    public boolean jingfeilaiyuandonjie(RecordSetTrans rst,Map<String, String>[] param, boolean flag) {
        logger.info("报销扣减明细表冻结扣减核批来源(会计节点后)");
        boolean boo = true;
        for (int i = 0; i < param.length; i++) {
            Budgetsources bud = new Budgetsources();
            //流程记录id
            String lcjlid = param[i].get("lcjlid");
            Integer zcqd = Integer.valueOf(param[i].get("zcqd"));
            String fzysrfl = param[i].get("fzysrfl");
            String xzkmdm = param[i].get("xzkmdm");
            String nd = param[i].get("nd");
            String sqbxje = param[i].get("money");
            Integer ysly = Integer.valueOf(param[i].get("ysly"));
            String zcdfczbkxm = param[i].get("zcdfczbkxm");
            String tybxje = param[i].get("tybxje");
            String cxje1 = param[i].get("cxje");
            //String dw = param[i].get("dw"); 此处dw为报销流程上的独立核算单位字段，改为从项目信息中获取
            String dw = getXmdw(lcjlid);
            String jbbm = param[i].get("jbbm");
            String gnkm = getuf_gnkmId(getuf_gxmdygnkmId(xzkmdm), nd);
            bud.setZcqd(zcqd);
            bud.setYsly(ysly);
            bud.setNd(nd);
            bud.setFzysrfl(fzysrfl);
            bud.setXzkmdm(xzkmdm);
            bud.setYsxm(lcjlid);
            bud.setTybxje(tybxje);
            bud.setGnkm(gnkm);
            bud.setZcdfczbkxm(zcdfczbkxm);
            bud.setSqbxje(sqbxje);
            bud.setCxje(cxje1);
            bud.setDw(dw);
            bud.setJbbm(jbbm);
            logger.info("预算来源lcjlid:" + lcjlid + ",支出渠道:" + zcqd + "预算来源:" + ysly + "非中央收入分类:" + fzysrfl + "选择科目代码:" + xzkmdm + ",单位:" + dw);
            //报销金额
            BigDecimal money = new BigDecimal(sqbxje);
            if (StringUtils.isBlank(cxje1)) {
                cxje1 = "0.00";
            }
            BigDecimal cxje = new BigDecimal(cxje1);
            money = money.subtract(cxje);
            try {
                boo = setyusuan(bud, money, flag);//冻结预算来源
                if (zcqd == 0) {//支出渠道-中央财政收入,收入类型-中央财政收入，中央科目级和下级的扣减
                    boo = setzyczzbk(rst,bud, money, flag);
                } else if (zcqd == 1 && fzysrfl.equals("8") || zcqd == 1 && fzysrfl.equals("5")) {//非中央财政收入-财政拨款
                    boo = setdfczbk(rst,bud, money, flag); //冻结地方财政拨款项目
                } else if (zcqd == 1) {
                    boo = setfzyczbk(rst,bud, money, flag);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return boo;
    }

    /**
     * TODO 归档前金额扣减（主表）
     * @param param
     * @param flag true 扣减 false退回释放
     * @return
     */
    public boolean jingfeilaiyuandonjie2(RecordSetTrans rst,Map<String, String> param, boolean flag) {
        logger.info("归档前金额扣减（主表）");
        boolean boo = true;
        String sqbxje = param.get("money");
        String tybxje = param.get("tybxje");
        String lcjlid = param.get("lcjlid");
        Integer zcqd = Integer.valueOf(param.get("zcqd"));
        String fzysrfl = param.get("fzysrfl");
        String xzkmdm = param.get("xzkmdm");
        String nd = param.get("nd");
        Integer ysly = Integer.valueOf(param.get("ysly"));
        String zcdfczbkxm = param.get("zcdfczbkxm");
        String cxje1 = param.get("cxje");
        String dw = param.get("dw");
        String jbbm = param.get("jbbm");
        Budgetsources bud = new Budgetsources();
        String gnkm = getuf_gnkmId(getuf_gxmdygnkmId(xzkmdm), nd);
        bud.setZcqd(zcqd);
        bud.setYsly(ysly);
        bud.setNd(nd);
        bud.setFzysrfl(fzysrfl);
        bud.setXzkmdm(xzkmdm);
        bud.setYsxm(lcjlid);
        bud.setTybxje(tybxje);
        bud.setGnkm(gnkm);
        bud.setSqbxje(sqbxje);
        bud.setZcdfczbkxm(zcdfczbkxm);
        bud.setDw(dw);
        bud.setJbbm(jbbm);
        logger.info("经办部门:"+jbbm+",预算来源lcjlid:" + lcjlid + ",支出渠道:" + zcqd + "预算来源:" + ysly + "非中央收入分类:" + fzysrfl + "选择科目代码:" + xzkmdm);
        //报销金额
        BigDecimal money = new BigDecimal(param.get("money"));
        if (StringUtils.isBlank(cxje1)){
            cxje1="0.00";
        }
        BigDecimal cxje = new BigDecimal(cxje1);
        money=money.subtract(cxje);
        try {
            boo = setyusuan(bud, money, flag);//冻结预算来源
            if (zcqd == 0) {
                //支出渠道-中央财政收入,收入类型-中央财政收入，中央科目级和下级的扣减
                boo = setzyczzbk(rst,bud, money, flag);
            } else if (zcqd == 1 && fzysrfl.equals("8") || zcqd == 1 && fzysrfl.equals("5")) {
                //非中央财政收入-财政拨款
                boo = setdfczbk(rst,bud, money, flag); //冻结地方财政拨款项目和其他支出
            } else if (zcqd == 1) {
                boo = setfzyczbk(rst,bud, money, flag);//冻结地方财政拨款非项目型
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return boo;
    }


    /**
     * @description: TODO 修改 预算来源冻结&剩余额度
     * @author
     * @date
     * @version 1.0
     */
    public static Boolean setyusuan(Budgetsources bud, BigDecimal money, boolean flag) {
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        RecordSet rs1 = JinfeiQuery(bud);
        String lcjlid = bud.getYsxm();
        String nd = bud.getNd();
        BigDecimal tybxje = new BigDecimal(bud.getTybxje());
        BigDecimal cxje = new BigDecimal(bud.getCxje());
        BigDecimal money1 = new BigDecimal(String.valueOf(money));
        BigDecimal money2 = new BigDecimal(0.00);
        tybxje = tybxje.subtract(cxje);
        boolean boo = true;
        try {
            while (rs1.next()) {
                BigDecimal syed = new BigDecimal(rs1.getString("syed"));//剩余额度
                BigDecimal djje = new BigDecimal(rs1.getString("djje"));//冻结金额
                BigDecimal hj = new BigDecimal(rs1.getString("hj"));//合计金额
                String id = rs1.getString("id");

                if (flag) { //true : 归档扣减 isReturn==0 不退回
                    djje = djje.subtract(money1);
                    money1 = money1.subtract(tybxje);
                    syed = syed.add(money1);
                } else {
                    syed = syed.subtract(money1);
                    djje = djje.add(money1);
                }

                String update = "update uf_jingfeilaiyuan set syed=" + syed.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",djje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where lcid=" + lcjlid + " and id=" + id + " and nd=" + nd;
                logger.info("明细表预算来源冻结sql:" + update + ",合计金额:" + hj.setScale(2, RoundingMode.HALF_UP).doubleValue() + "报销金额:" + tybxje.setScale(2, RoundingMode.HALF_UP).doubleValue());
                boo = rst.executeUpdate(update);
                logger.info("明细表预算来源冻结 "+boo);
                rst.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
            rst.rollback();
        }
        return boo;
    }


    /**
     * @description: TODO 中央财政拨款收入——科目or下级 扣减
     * @author
     * @date
     * @version 1.0
     */
    public static boolean setzyczzbk(RecordSetTrans rst,Budgetsources bud, BigDecimal money, boolean flag) {
        String uf_km = "uf_zyczbksr_km";
        String uf_xj = "uf_zyczbksr_xj";
        BaseBean baseBean = new BaseBean();
        int yj = Integer.parseInt(baseBean.getPropValue("Verification","yj"));//科目级
        int xj = Integer.parseInt(baseBean.getPropValue("Verification","xj"));//下级
        //logger.info("科目级yj:"+yj+",下级xj:"+xj);
        int sum1 = 0;
        int sum2 = 0;
        boolean boo = true;
        RecordSet rs = zyJinfeiQuery(bud);
        while (rs.next()) {
            String zyczbksr = rs.getString("hpssjid");//中央财政拨款收入_核批时数据id
            String kmjxj = rs.getString("kmjxj");//0:科目级，1:下级
            logger.info("中央财政拨款收入_核批时数据id:"+zyczbksr+"科目级/下级:"+kmjxj);
            if (StringUtils.isNotBlank(zyczbksr)) {
                String[] sta = zyczbksr.split("_");//当年结转核批下划线分割
                sum1 = Integer.parseInt(sta[0]);//9为科目级，10为科目下级
                sum2 = Integer.parseInt(sta[1]);//表id
                if (sum1 == yj) {
                    boo = setzyczzbk(rst,bud, money, uf_km, sum2,kmjxj,flag);//只扣科目级
                } else if (sum1 == xj) {
                    Boolean boo1=true;
                    List<String> superiorId = getSuperiorId(sum2);
                    String joinId = Joiner.on(",").join((Iterable<?>) superiorId);
                    for (int i = 0 ; i<joinId.split(",").length;i++){
                        String id = joinId.split(",")[i];
                        boo1 = setzyczzbk(rst,bud, money, uf_xj, Integer.valueOf(id),kmjxj, flag);//扣减所有科目下级
                        logger.info(" 扣减所有科目下级 所有下级id"+id+" 扣减结果："+boo1);
                    }

                    //这里为什么还要扣科目级
                    /*
                    RecordSet zyczxj = getZyczxj(sum2);
                    zyczxj.next();
                    Integer szgnkm = zyczxj.getInt("szgnkm");
                    boolean boo2 = setzyczzbk(bud, money, uf_km, szgnkm, kmjxj,flag);//扣减科目级
                    if (!boo1 || !boo2) {
                        return false;
                    }
                    */
                }
            }
        }
        return boo;
    }
    public static List<String> getSuperiorId(Integer id){
        List<String> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = " select sjzxsr from  uf_zyczbksr_xj where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        if (StrUtil.isNotEmpty(rs.getString("sjzxsr"))){
            List<String> sjzxsr = getSuperiorId(rs.getInt("sjzxsr"));
            list.addAll(sjzxsr);
        }
        list.add(String.valueOf(id));
        return list;
    }
    public static RecordSet getZyczxj(Integer id){
        RecordSet rs=new RecordSet();
        String zyxjSql = "select szgnkm,srzx from uf_zyczbksr_xj where id=" + id;
        rs.executeQuery(zyxjSql);
        logger.info("查询中央财政收入下级所在功能科目sql:"+zyxjSql);
        return rs;
    }
    /**
     * @description: TODO 中央财政拨款收入——科目 扣减
     * @author
     * @date
     * @version 1.0
     */
    public static boolean setzyczzbk(RecordSetTrans rst,Budgetsources bud, BigDecimal money,String tableName,Integer id,String kmjxj, boolean flag) {
        //uf_zyczbksr_km
        //RecordSetTrans rst = new RecordSetTrans();
        //rst.setAutoCommit(false);
        Integer ysly = bud.getYsly();
        BigDecimal tybxje = new BigDecimal(bud.getTybxje());
        BigDecimal cxje = new BigDecimal(bud.getCxje());
        tybxje = tybxje.subtract(cxje);
        String sql="";
        if (kmjxj.equals("0")){//0:科目级,1:下级
            sql = "select djje,sjbxje,snjzhpbxje,snjzhpsyje,hpsyje,snjzhpdjje from "+tableName+" where 1=1 and id ="+id;
        }else {
            sql = "select djje,sjbxje,snjzhpbxje,snjzhpsyje,yhpsy,snjzhpdjje from "+tableName+" where 2=2 and id ="+id;
        }
        logger.info("中央财政拨款收入——科目查询sql:"+sql);
        boolean boo = true;
        try {
            rst.executeQuery(sql);
            while (rst.next()) {
                String sjbxje1 = rst.getString("sjbxje");
                if (StringUtils.isBlank(sjbxje1)) {
                    sjbxje1 = "0.00";
                }
                String je="";
                if (kmjxj.equals("0")){
                    je=rst.getString("hpsyje");
                }else {
                    je=rst.getString("yhpsy");
                }
                if (StringUtils.isBlank(je)) {
                    je = "0.00";
                }
                String djje1="";
                if (ysly==1){//上年冻结金额
                    djje1 = rst.getString("snjzhpdjje");
                }else {//当年结转
                    djje1 = rst.getString("djje");
                }
                if (StringUtils.isBlank(djje1)) {
                    djje1 = "0.00";
                }
                String snjzhpbxje1 = rst.getString("snjzhpbxje");
                if (StringUtils.isBlank(snjzhpbxje1)) {
                    snjzhpbxje1 = "0.00";
                }
                String snjzhpsyje1 = rst.getString("snjzhpsyje");
                if (StringUtils.isBlank(snjzhpsyje1)) {
                    snjzhpsyje1 = "0.00";
                }
                BigDecimal sjbxje = new BigDecimal(sjbxje1);//实际报销金额
                BigDecimal hpsyje = new BigDecimal(je);//核批剩余金额
                BigDecimal djje = new BigDecimal(djje1);//冻结金额
                BigDecimal snjzhpbxje = new BigDecimal(snjzhpbxje1);//上年结转核批报销金额
                BigDecimal snjzhpsyje = new BigDecimal(snjzhpsyje1);//上年结转核批剩余金额
                String updateSql = "";
                if (ysly == 0) {//0:当年
                    //huangwm注释 取消核批剩余金额的判断 2024年6月3日重新注释 看会不会被还原
//                    if (hpsyje.compareTo(BigDecimal.ZERO) == -1 || hpsyje.compareTo(BigDecimal.ZERO) == 0) {
//                        logger.info("当年预算金额不足");
//                        boo = false;
//                        return boo;
//                    }
                    if (flag) {//true:归档扣减
                        djje = djje.subtract(money);//冻结金额减去申请金额
                        money = money.subtract(tybxje);//计算申请金额跟同意金额的差额
                        hpsyje = hpsyje.add(money);//核批剩余金额加上差额
                        sjbxje = sjbxje.subtract(money);//实际报销金额减去差额
                    } else {
                        hpsyje = hpsyje.subtract(money);
                        djje = djje.add(money);
                        sjbxje = sjbxje.add(money);
                    }
                    if (kmjxj.equals("0")){
                        logger.info("科目级，tableName="+tableName);
                        updateSql = "update "+tableName+" set hpsyje=" + hpsyje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",djje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",sjbxje=" + sjbxje.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where id="+id;
                    }else {
                        logger.info("科目下级，tableName="+tableName);
                        updateSql = "update "+tableName+" set yhpsy=" + hpsyje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",djje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",sjbxje=" + sjbxje.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where id="+id;
                    }
                } else if (ysly == 1) {//上年
                    if (snjzhpsyje.compareTo(BigDecimal.ZERO) == -1 || snjzhpsyje.compareTo(BigDecimal.ZERO) == 0) {
                        logger.info("上年结转金额不足");
                        boo = false;
                        return boo;
                    }
                    if (flag) {//true:归档扣减
                        djje = djje.subtract(money);//冻结金额减去申请金额
                        money = money.subtract(tybxje);//计算申请金额跟同意金额的差额
                        snjzhpsyje = snjzhpsyje.add(money);//上年结转核批剩余金额加上差额
                        snjzhpbxje = snjzhpbxje.subtract(money);//上年结转核批报销金额减去差额
                    } else {
                        snjzhpsyje = snjzhpsyje.subtract(money);
                        snjzhpbxje = snjzhpbxje.add(tybxje);
                        djje = djje.add(money);
                    }
                    updateSql = "update "+tableName+" set snjzhpsyje=" + snjzhpsyje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",snjzhpdjje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",snjzhpbxje=" + snjzhpbxje.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where id="+id;
                }
                logger.info("中央财政拨款收入扣减sql：" + updateSql);
                boo = rst.executeUpdate(updateSql);
                logger.info("中央财政拨款收入扣减结果：" + boo);
                //rst.commit(); 统一由action提交
            }
        } catch (Exception e) {
            logger.info(" 中央财政拨款收入——科目 扣减异常 "+ e.getMessage());
            e.printStackTrace();
            rst.rollback();
        }
        return boo;
    }

    /**
     * @description: TODO 修改 地方财政拨款项目冻结&剩余额度
     * @author
     * @date
     * @version 1.0
     */
    public static boolean setdfczbk(RecordSetTrans rst,Budgetsources bud, BigDecimal money, boolean flag) {
        logger.info("地方财政拨款项目冻结&剩余额度");
        //RecordSetTrans rst = new RecordSetTrans();
        //rst.setAutoCommit(false);
        String zcdfczbkxm = bud.getZcdfczbkxm();
        Integer ysly = bud.getYsly();
        BigDecimal tybxje = new BigDecimal(bud.getTybxje());
        BigDecimal cxje = new BigDecimal(bud.getCxje());
        String dw = bud.getDw();
        tybxje = tybxje.subtract(cxje);
        String sql = "select sjsrsy,djje,snjzhpbxje,snjzhpsyje,snjzhpdjje,sjbxje from uf_fzysrxm where id=" + zcdfczbkxm + " and szdw=" + dw;
        logger.info("查询sql>>"+sql);
        boolean boo = true;
        try {
            rst.executeQuery(sql);
            while (rst.next()) {
                String sjbxje1 = rst.getString("sjbxje");
                if (StringUtils.isBlank(sjbxje1)) {
                    sjbxje1 = "0.00";
                }
                String sjsrsy1 = rst.getString("sjsrsy");
                if (StringUtils.isBlank(sjsrsy1)) {
                    sjsrsy1 = "0.00";
                }
                String djje1="";
                if (ysly==1){//上年冻结金额
                    djje1 = rst.getString("snjzhpdjje");
                }else {//当年结转
                    djje1 = rst.getString("djje");
                }
                if (StringUtils.isBlank(djje1)) {
                    djje1 = "0.00";
                }
                String snjzhpbxje1 = rst.getString("snjzhpbxje");
                if (StringUtils.isBlank(snjzhpbxje1)) {
                    snjzhpbxje1 = "0.00";
                }
                String snjzhpsyje1 = rst.getString("snjzhpsyje");
                if (StringUtils.isBlank(snjzhpsyje1)) {
                    snjzhpsyje1 = "0.00";
                }
                BigDecimal sjsrsy = new BigDecimal(sjsrsy1);//实际收入剩余
                BigDecimal djje = new BigDecimal(djje1);//冻结金额
                BigDecimal sjbxje = new BigDecimal(sjbxje1);//实际报销金额
                BigDecimal snjzhpsyje = new BigDecimal(snjzhpsyje1);//上年结转核批剩余金额
                BigDecimal snjzhpbxje = new BigDecimal(snjzhpbxje1);//上年结转核批报销金额
                String update = "";
                if (ysly == 0) {
                    //2024年4月15日 huangwm  取消实际收入判断
//                    if (sjsrsy.compareTo(BigDecimal.ZERO) == 0 || sjsrsy.compareTo(BigDecimal.ZERO) == -1) {
//                        logger.info("实际收入剩余金额不足");
//                        boo = false;
//                        return boo;
//                    }
                    if (flag) {//归档扣减
                        djje = djje.subtract(money);//冻结金额减掉申请金额
                        money = money.subtract(tybxje);//得出申请金额跟同意金额的差额
                        sjsrsy = sjsrsy.add(money);//实际收入剩余加上差额
                        sjbxje = sjbxje.subtract(money);//实际报销金额减去差额
                    } else {//冻结扣减
                        sjsrsy = sjsrsy.subtract(money);
                        sjbxje = sjbxje.add(money);
                        djje = djje.add(money);
                    }
                    update = "update uf_fzysrxm set sjsrsy=" + sjsrsy.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",djje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",sjbxje=" + sjbxje.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where id=" + zcdfczbkxm + " and szdw=" + dw;
                } else if (ysly == 1) {
                    if (snjzhpsyje.compareTo(BigDecimal.ZERO) == 0 || snjzhpsyje.compareTo(BigDecimal.ZERO) == -1) {
                        logger.info("以前年度结转实际金额不足");
                        //2024年3月26日 huangwm 取消结转金额判断
                        //boo = false;
                        //return boo;
                    }
                    if (flag) {//归档扣减
                        djje = djje.subtract(money);//冻结金额减去申请金额
                        money = money.subtract(tybxje);//计算申请金额跟同意金额的差额
                        snjzhpsyje = snjzhpsyje.add(money);//上年结转核批剩余金额加上差额
                        snjzhpbxje = snjzhpbxje.subtract(money);//上年结转报销金额减去差额
                    } else {//冻结
                        snjzhpsyje = snjzhpsyje.subtract(money);
                        snjzhpbxje = snjzhpbxje.add(tybxje);
                        djje = djje.add(money);
                    }
                    update = "update uf_fzysrxm set snjzhpsyje=" + snjzhpsyje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",snjzhpbxje" + snjzhpbxje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",snjzhpdjje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where id=" + zcdfczbkxm + " and szdw=" + dw;
                }
                logger.info("地方财政拨款项目扣减sql:" + update);
                boo = rst.executeUpdate(update);
                logger.info("地方财政拨款项目扣减结果:" + boo);
                //rst.commit();统一由action提交
            }
        } catch (Exception e) {
            e.printStackTrace();
            rst.rollback();
        }
        return boo;
    }

    /**
     * @description: TODO 修改 非财政拨款项目冻结&剩余额度
     * @author
     * @date
     * @version 1.0
     */
    public static boolean setfzyczbk( RecordSetTrans rst,Budgetsources bud, BigDecimal money, boolean flag) {
        logger.info("非财政拨款项目冻结&剩余额度");
//        RecordSetTrans rst = new RecordSetTrans();
//        rst.setAutoCommit(false);
        String fzysrfl = bud.getFzysrfl();
        Integer ysly = bud.getYsly();
        String nd = bud.getNd();
        String dw = bud.getDw();
        BigDecimal tybxje = new BigDecimal(bud.getTybxje());
        BigDecimal cxje = new BigDecimal(bud.getCxje());
        tybxje = tybxje.subtract(cxje);
        String sql = "select djje,snjzhpbxje,snjzhpsyje,snjzhpdjje,sjbxje,sjsrsy from uf_fzysr where szfl=" + fzysrfl + " and nf=" + nd + " and dw=" + dw;
        logger.info("查询sql>>" + sql);
        boolean boo = true;
        try {
            rst.executeQuery(sql);
            while (rst.next()) {
                String sjbxje1 = rst.getString("sjbxje");
                if (StringUtils.isBlank(sjbxje1)) {
                    sjbxje1 = "0.00";
                }
                String sjsrsy1 = rst.getString("sjsrsy");
                if (StringUtils.isBlank(sjsrsy1)) {
                    sjsrsy1 = "0.00";
                }
                String djje1="";
                if (ysly==1){//上年冻结金额
                    djje1 = rst.getString("snjzhpdjje");
                }else {//当年结转
                    djje1 = rst.getString("djje");
                }
                if (StringUtils.isBlank(djje1)) {
                    djje1 = "0.00";
                }
                String snjzhpbxje1 = rst.getString("snjzhpbxje");
                if (StringUtils.isBlank(snjzhpbxje1)) {
                    snjzhpbxje1 = "0.00";
                }
                String snjzhpsyje1 = rst.getString("snjzhpsyje");
                if (StringUtils.isBlank(snjzhpsyje1)) {
                    snjzhpsyje1 = "0.00";
                }
                BigDecimal sjsrsy = new BigDecimal(sjsrsy1);//实际收入
                BigDecimal djje = new BigDecimal(djje1);//冻结金额
                BigDecimal sjbxje = new BigDecimal(sjbxje1);//实际报销金额
                BigDecimal snjzhpsyje = new BigDecimal(snjzhpsyje1);//上年结转核批剩余金额
                BigDecimal snjzhpbxje = new BigDecimal(snjzhpbxje1);//上年结转核批报销金额
                String update = "";
                if (ysly == 0) {
                    if (sjsrsy.compareTo(BigDecimal.ZERO) == -1 || sjsrsy.compareTo(BigDecimal.ZERO) == 0) {
                        logger.info("实际收入金额不足");
                        logger.info("20240124 huangwm去掉实际收入的控制 同下考虑");
//                        boo = false;
//                        return boo;
                    }
                    if (flag) {//归档扣减
                        djje = djje.subtract(money);//冻结金额减去申请金额
                        money = money.subtract(tybxje);//计算申请金额跟同意金额的差额
                        sjsrsy = sjsrsy.add(money);//实际收入剩余加上差额
                        sjbxje = sjbxje.subtract(money);//实际报销金额减去差额
                    } else {//冻结
                        sjsrsy = sjsrsy.subtract(money);
                        djje = djje.add(money);
                        sjbxje = sjbxje.add(money);
                    }
                    update = "update uf_fzysr set sjsrsy=" + sjsrsy.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",djje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",sjbxje=" + sjbxje.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where szfl=" + fzysrfl + " and dw=" + dw;
                } else if (ysly == 1) {
                    //去掉这个限制 原本用于判断核批的剩余额度，现在考虑的是审批了预算额度就控制项目的渠道额度即可 2023年8月25日
//                    if (snjzhpsyje.compareTo(BigDecimal.ZERO) == -1 || snjzhpsyje.compareTo(BigDecimal.ZERO) == 0) {
//                        logger.info("以前年度实际结转实际收入金额不足");
//                        boo = false;
//                        return boo;
//                    }
                    if (flag) {//归档扣减
                        djje = djje.subtract(money);//冻结金额减去申请金额
                        money = money.subtract(tybxje);//计算申请金额跟同意金额的差额
                        snjzhpsyje = snjzhpsyje.add(money);//上年结转核批剩余金额加上差额
                        snjzhpbxje = snjzhpbxje.subtract(money);//上年结转报销金额金额减去差额
                    } else {//冻结扣减
                        snjzhpbxje = snjzhpbxje.add(money);
                        snjzhpsyje = snjzhpsyje.subtract(money);
                        djje = djje.add(money);
                    }
                    update = "update uf_fzysr set snjzhpsyje=" + snjzhpsyje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",snjzhpdjje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",snjzhpbxje=" + snjzhpbxje.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where szfl=" + fzysrfl  + " and dw=" + dw;
                }
                logger.info("非财政拨款项目冻结sql:" + update);
                boo = rst.executeUpdate(update);
                logger.info("非财政拨款项目冻结结果:" + update);
                //rst.commit(); 统一由action提交
            }
        } catch (Exception e) {
            e.printStackTrace();
            //rst.rollback();
        }
        return boo;
    }

    /**
     * @description: TODO 使用单位额度冻结扣减-明细表
     * @author
     * @date
     * @version 1.0
     */
    public boolean userQuotaFreezeDeduction(RecordSetTrans rst,Map<String, String>[] param, boolean flag) {
        logger.info("明细表使用单位额度冻结扣减");
        //RecordSetTrans rst = new RecordSetTrans();
        //rst.setAutoCommit(false);
        boolean boo = true;
        for (int i = 0; i < param.length; i++) {
            BigDecimal money = new BigDecimal(param[i].get("money"));
            BigDecimal cxje = new BigDecimal(param[i].get("cxje"));//冲销金额
            BigDecimal tybxje = new BigDecimal(param[i].get("tybxje"));//同意报销金额
            String lcjlid = param[i].get("lcjlid");
            String bxr = param[i].get("bxr");
            String sydw = getsubcompanyId(bxr);
            String sql = "select djje,syed,ysyed from uf_ysxmsydwfped where xmbzlc=" + lcjlid + " and sydw=" + sydw;
            logger.info("查询sql:" + sql);
            try {
                rst.executeQuery(sql);
                money = money.subtract(cxje);
                while (rst.next()) {
                    String djje1 = rst.getString("djje");
                    String syed1 = rst.getString("syed");
                    String ysyed1 = rst.getString("ysyed");
                    if (StringUtils.isBlank(djje1)) {
                        djje1 = "0.00";
                    }
                    if (StringUtils.isBlank(syed1)) {
                        syed1 = "0.00";
                    }
                    if (StringUtils.isBlank(ysyed1)) {
                        ysyed1 = "0.00";
                    }
                    BigDecimal djje = new BigDecimal(djje1);
                    BigDecimal syed = new BigDecimal(syed1);
                    BigDecimal ysyed = new BigDecimal(ysyed1);
                    if (flag) {//true：归档扣减
                        djje = djje.subtract(money);//冻结金额减去申请金额
                        tybxje = tybxje.subtract(cxje);//同意报销金额减去冲销金额
                        money = money.subtract(tybxje);//计算申请金额跟上一步后的差额
                        syed = syed.add(money);//剩余额度加上最终差额
                        ysyed = ysyed.subtract(money);//已使用额度减去最终差额
                    } else {
                        djje = djje.add(money);
                        syed = syed.subtract(money);
                        ysyed = ysyed.add(money);
                    }
                    String updateSql = "update uf_ysxmsydwfped set djje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",syed=" + syed.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",ysyed=" + ysyed.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where xmbzlc=" + lcjlid + " and sydw=" + sydw;
                    logger.info("预算项目使用单位分配额度冻结Sql" + updateSql);
                    boo = rst.executeUpdate(updateSql);
                    logger.info("预算项目使用单位分配额度冻结结果" + boo);
                    //rst.commit(); 统一由action 提交
                }
            } catch (Exception e) {
                e.printStackTrace();
                rst.rollback();
            }
        }
        return boo;
    }

    /**
     * 判断使用单位分配额度 并且扣减额度
     * @param param
     * @param flag
     * @return
     */
    public boolean userQuotaFreezeDeduction(RecordSetTrans rst,Map<String, String> param, boolean flag) {
        logger.info("主表判断使用单位分配额度 并且扣减额度");
//        RecordSetTrans rst = new RecordSetTrans();
//        rst.setAutoCommit(false);
        boolean boo = true;
        BigDecimal money = new BigDecimal(param.get("money"));
        BigDecimal cxje = new BigDecimal(param.get("cxje"));//冲销金额
        BigDecimal tybxje = new BigDecimal(param.get("tybxje"));//同意报销金额
        String lcjlid = param.get("lcjlid");
        String bxr = param.get("bxr");
        String sydw = getsubcompanyId(bxr);
        money = money.subtract(cxje);
        String sql = "select djje,syed,ysyed from uf_ysxmsydwfped where xmbzlc=" + lcjlid + " and sydw=" + sydw;
        logger.info("预算项目使用单位分配额度sql:" + sql);
        try {
            rst.executeQuery(sql);
            while (rst.next()) {
                String djje1 = rst.getString("djje");
                String syed1 = rst.getString("syed");
                String ysyed1 = rst.getString("ysyed");
                if (StringUtils.isBlank(djje1)) {
                    djje1 = "0.00";
                }
                if (StringUtils.isBlank(syed1)) {
                    syed1 = "0.00";
                }
                if (StringUtils.isBlank(ysyed1)) {
                    ysyed1 = "0.00";
                }
                BigDecimal djje = new BigDecimal(djje1);
                BigDecimal syed = new BigDecimal(syed1);
                BigDecimal ysyed = new BigDecimal(ysyed1);
                if (flag) {//true：归档扣减
                    djje = djje.subtract(money);
                    tybxje = tybxje.subtract(cxje);
                    money = money.subtract(tybxje);
                    syed = syed.add(money);//剩余额度加上最终差额
                    ysyed = ysyed.subtract(money);//已使用额度减去最终差额
                } else {
                    djje = djje.add(money);
                    syed = syed.subtract(money);
                    ysyed = ysyed.add(money);
                }
                String updateSql = "update uf_ysxmsydwfped set djje=" + djje.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",syed=" + syed.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",ysyed=" + ysyed.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where xmbzlc=" + lcjlid + " and sydw=" + sydw;
                logger.info("预算项目使用单位分配额度冻结Sql" + updateSql);
                boo = rst.executeUpdate(updateSql);
                logger.info("预算项目使用单位分配额度冻结结果" + boo);
                //rst.commit(); 统一由action提交
            }
        } catch (Exception e) {
            e.printStackTrace();
            rst.rollback();
        }
        return boo;
    }


    //方法已废弃
    public  boolean modifyFreezeMoney_fq(String lcjlid,String bmzcjjfl,BigDecimal moneyDj,BigDecimal dj2,boolean flag){
        RecordSet rs=new RecordSet();
        String querySql="SELECT syed,dj FROM uf_jingfei  WHERE lcjlid="+lcjlid+" AND bmzcjjfl="+bmzcjjfl+" ";
        rs.executeQuery(querySql);
        boolean b=true;
        String modifySql="";
        while (rs.next()) {
            BigDecimal syed=new BigDecimal(rs.getString("syed"));
            BigDecimal dj=new BigDecimal(rs.getString("dj"));
            if (flag) {//退回
                dj=dj.subtract(moneyDj);
                syed=syed.add(moneyDj);
                modifySql = "update uf_jingfei set dj=" + dj.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",syed=" + syed.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where lcjlid=" + lcjlid + " and bmzcjjfl=" + bmzcjjfl;
            }else {//冻结
                modifySql = "update uf_jingfei set dj=" + dj2.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",syed+=" + moneyDj.setScale(2, RoundingMode.HALF_UP).doubleValue() + " where lcjlid=" + lcjlid + " and bmzcjjfl=" + bmzcjjfl;
            }
            logger.info("modifySql---"+modifySql);
            b = rs.executeUpdate(modifySql);
        }
        return b;
    }

    /**
     * @description: TODO 明细表金额扣减冻结（uf_jingfei）
     * @author
     * @date
     * @version 1.0
     */
    public boolean queryJingfei(RecordSetTrans rst,Map<String, String>[] param,Boolean flag,int isReturn) {
        logger.info("queryJingfei-->报销明细金额扣减 冻结 释放"+isReturn);
        boolean b = false;
        //RecordSetTrans rst = null;
        logger.info(JSONObject.toJSONString(param));
        for (int i = 0; i < param.length; i++) {
            try {
                //rst = new RecordSetTrans();
                //rst.setAutoCommit(false);
                //String mapString = getMapToString(param[i]);
                //logger.info("明细表金额扣减冻结 map参数"+i+"："+mapString);
                //流程记录id
                String lcjlid = param[i].get("lcjlid");
                String bmjjzcfl = param[i].get("bmjjzcfl"); //部门支出经济分类
                String tybxje1 = param[i].get("money");;//报销金额 huangwm
                if (isReturn==0) {
                    String workflowid = param[i].get("workflowid");
                    String WFID29 = param[i].get("userFudsactiveWFID29");
                    String WFID30 = param[i].get("userFudsactiveWFID30");
                    String WFID31 = param[i].get("userFudsactiveWFID31");
                    String WFID32 = param[i].get("userFudsactiveWFID32");
                    String WFID33 = param[i].get("userFudsactiveWFID33");
                    List<String> list = new ArrayList<>();
                    list.add(WFID29);
                    list.add(WFID30);
                    list.add(WFID31);
                    list.add(WFID32);
                    list.add(WFID33);
                    boolean boo = list.contains(workflowid);
                    if (boo) {
                        tybxje1 = param[i].get("money");
                        logger.info("人员经费 tybxje1》"+tybxje1);
                    } else {
                        tybxje1 = param[i].get("tybxje");
                        logger.info("非人员经费 tybxje1》"+tybxje1);
                    }
                    if (StringUtils.isBlank(tybxje1)) {
                        tybxje1 = "0.00";
                    }
                }
                //判断冲销金额为空
                String cxjeStr = param[i].get("cxje");
                if (StringUtils.isBlank(cxjeStr)) {
                    cxjeStr = "0.00";
                }
                BigDecimal sqbxje = new BigDecimal(param[i].get("money"));
                BigDecimal tybxje = new BigDecimal(tybxje1);
                BigDecimal cxje = new BigDecimal(cxjeStr);
                BigDecimal ybx_tybxje = new BigDecimal(param[i].get("tybxje"));
                sqbxje = sqbxje.subtract(cxje);
                logger.info("同意报销金额:" + tybxje1 + ",申请报销金额:" + sqbxje + "冲销金额:" + cxje);
                String querySql = "SELECT bmzcjjfl,ze,dj, syed,isnull(ybx,0)as ybx FROM uf_jingfei  WHERE lcjlid=" + lcjlid + " AND bmzcjjfl=" + bmjjzcfl + " ";
                logger.info("明细表金额扣减冻结 querySql-->"+querySql);

                rst.executeQuery(querySql);
                //logger.info("明细表金额扣减冻结 try catech-->"+i);
                if (rst.next()) {
                    //logger.info("明细表金额扣减冻结 querySql"+i+" 有结果");
                    BigDecimal syed = new BigDecimal(rst.getDouble("syed"));
                    BigDecimal dj = new BigDecimal(rst.getDouble("dj"));
                    BigDecimal ybx = new BigDecimal(rst.getDouble("ybx"));
                    //logger.info("syed>>"+syed+"   dj>>"+dj+" ybx>>"+ybx);
                    if (isReturn==0) {//不退回
                        if (flag) {//归档扣减
                            dj = dj.subtract(sqbxje);//归档冻结金额减掉还回去
                            tybxje = tybxje.subtract(cxje);//得出同意报销金额减去冲销金额的差额
                            sqbxje = sqbxje.subtract(tybxje);//得出同意报销金额跟申请报销金额的差为实际报销金额
                            syed = syed.add(sqbxje);//sqbxje最后得出来的一般为正数 因此要加回去
                            ybx = ybx.add(ybx_tybxje);//已报销金额加上本次同意报销金额
                        } else {//冻结
                            syed = syed.subtract(sqbxje);
                            dj = dj.add(sqbxje);
                        }
                    } else if (isReturn==1) {//退回
                        syed = syed.add(sqbxje);
                        dj = dj.subtract(sqbxje);
                    }
                    //logger.info("syed>>"+syed+"   dj>>"+dj+" ybx>>"+ybx);
                    String updateSql = "update uf_jingfei set syed=" + syed.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",dj=" + dj.setScale(2, RoundingMode.HALF_UP).doubleValue() +",ybx="+ybx.setScale(2, RoundingMode.HALF_UP).doubleValue()+ " where lcjlid=" + lcjlid + " and bmzcjjfl=" + bmjjzcfl + "";
                    b = rst.executeUpdate(updateSql);
                    logger.info("经费表冻结金额Sql：-------------------->" + updateSql);
                    //rst.commit(); 统一由action提交
                }
                logger.info("经费表冻结金额结果1：-------------------->" + b);
                //return b;
            } catch (Exception e) {
                logger.info("queryJingfei error  "+e.getMessage());
                e.printStackTrace();
                rst.rollback();
            }
        }
        logger.info("经费表冻结金额结果2：-------------------->" + b);
        return b;
    }

    /**
     * TODO 主表金额扣减冻结（uf_jingfei）
     * @param param
     * @param flag
     * @param isReturn 标记是否退回 0扣减 1退回释放
     * @return
     */
    public boolean queryMainJingfei(RecordSetTrans rst,Map<String, String> param, Boolean flag,int isReturn) {
        logger.info("queryMainJingfei-->报销主表金额扣减 冻结  释放"+isReturn);
//        RecordSetTrans rst = new RecordSetTrans();
//        rst.setAutoCommit(false);
        String mapString = getMapToString(param);
        logger.info("map参数："+mapString);
        boolean b = false;
        //流程记录id
        String lcjlid = param.get("lcjlid");
        //部门支出经济分类
        String bmjjzcfl = param.get("bmjjzcfl");
        //报销金额
        String tybxje1 = param.get("tybxje");
        if (StringUtils.isBlank(tybxje1)) {
            tybxje1 = "0.00";
        }
        BigDecimal sqbxje = new BigDecimal(param.get("money"));
        BigDecimal tybxje = new BigDecimal(tybxje1);
        BigDecimal cxje = new BigDecimal(param.get("cxje"));
        BigDecimal ybx_tybxje =  new BigDecimal(param.get("tybxje"));
        sqbxje = sqbxje.subtract(cxje);
        logger.info("同意报销金额:" + tybxje1 + ",申请报销金额:" + sqbxje + "冲销金额:" + cxje);
        String querySql = "SELECT bmzcjjfl,ze,dj, syed,isnull(ybx,0)as ybx FROM uf_jingfei  WHERE lcjlid=" + lcjlid + " AND bmzcjjfl=" + bmjjzcfl + " ";
        logger.info("主表金额扣减冻结 querySql："+querySql);
        try {
            rst.executeQuery(querySql);
            if (rst.next()) {
                BigDecimal syed = new BigDecimal(rst.getDouble("syed"));
                BigDecimal dj = new BigDecimal(rst.getDouble("dj"));
                BigDecimal ybx = new BigDecimal(rst.getDouble("ybx"));
                logger.info("查询syed："+syed+"   查询dj："+dj);
                if (isReturn==0) {
                    if (flag) {//归档扣减
                        dj = dj.subtract(sqbxje);
                        tybxje = tybxje.subtract(cxje);
                        sqbxje = sqbxje.subtract(tybxje);
                        syed = syed.add(sqbxje);
                        ybx = ybx.add(ybx_tybxje);//已报销金额加上本次同意报销金额
                    } else {//冻结
                        syed = syed.subtract(sqbxje);
                        dj = dj.add(sqbxje);
                    }
                }else if (isReturn==1){//退回
                    syed = syed.add(sqbxje);
                    dj = dj.subtract(sqbxje);
                }
                String updateSql = "update uf_jingfei set syed=" + syed.setScale(2, RoundingMode.HALF_UP).doubleValue() + ",dj=" + dj.setScale(2, RoundingMode.HALF_UP).setScale(2, RoundingMode.HALF_UP).doubleValue() +",ybx="+ybx.setScale(2, RoundingMode.HALF_UP).setScale(2, RoundingMode.HALF_UP).doubleValue()+ " where lcjlid=" + lcjlid + " and bmzcjjfl=" + bmjjzcfl + "";
                logger.info("经费表冻结释放金额：-------------------->" + updateSql);
                b = rst.executeUpdate(updateSql);
                //rst.commit(); 统一由action提交
            }
        } catch (Exception e) {
            e.printStackTrace();
            rst.rollback();
        }
        logger.info("经费表冻结释放结果：-------------------->" + b);
        return b;
    }

    public static RecordSet getzyczKm(Integer id) {
        RecordSet rs = new RecordSet();
        String sql="select kmdm from uf_zyczbksr_km where id="+id;
        logger.info("获取中央财政科目级科目名称sql:"+sql);
        rs.executeQuery(sql);
        return rs;
    }
    public static RecordSet getDfczbkName(String id) {
        RecordSet rs = new RecordSet();
        String sql="select xmmc from uf_fzysrxm where id="+id;
        logger.info("获取地方财政拨款项目名称sql:"+sql);
        rs.executeQuery(sql);
        return rs;
    }
    public static RecordSet getFzyslflName(String id) {
        RecordSet rs = new RecordSet();
        String sql="select mc from uf_srysfl where id="+id;
        logger.info("获取非中央收入分类名称sql:"+sql);
        rs.executeQuery(sql);
        return rs;
    }
    /**
     *   TODO 获取、排序、比较两个Map中相同key对应value值
     * @param hashMap 原数据
     * @param hashMap2 需要比较的数据
     * @return
     */
    public boolean mapCompar(HashMap<String,String> hashMap,HashMap<String,String> hashMap2) {
        boolean isChange = false;
        for (Map.Entry<String, String> entry1 : hashMap.entrySet()) {
            String m1value = entry1.getValue() == null ? "" :  entry1.getValue();
            String m2value = hashMap2.get(entry1.getKey()) == null ? "" :  hashMap2.get(entry1.getKey());
            if (!m1value.equals(m2value)) {
                isChange = true;
            }
            logger.info("isChange"+isChange);
        }
        return isChange;
    }

    /**
     * Map转String
     * @param map
     * @return
     */
    public  String getMapToString(Map<String,String> map){
        Set<String> keySet = map.keySet();
        //将set集合转换为数组
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        //给数组排序(升序)
        Arrays.sort(keyArray);
        //因为String拼接效率会很低的，所以转用StringBuilder
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (int i = 0; i < keyArray.length; i++) {
            // 参数值为空，则不参与签名 这个方法trim()是去空格
            if ((String.valueOf(map.get(keyArray[i]))).trim().length() > 0) {
                sb.append(keyArray[i]).append(":").append(String.valueOf(map.get(keyArray[i])).trim());
            }
            if(i != keyArray.length-1){
                sb.append(",");
            }
        }
        sb.append("}");
        return sb.toString();
    }

}
